package com.infomanage.service.worklog;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.AttendanceDAO;
import com.infomanage.enums.AttendanceResultType;
import com.infomanage.util.StringUtil;
import com.infomanage.util.TimeUtil;
import com.infomanage.vo.AttendanceItem;

public class AttendanceStaticsService {
	
	public static HashMap<AttendanceItem, HashMap<Integer, String>> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			AttendanceDAO dao = new AttendanceDAO();
			String staticSearch = request.getParameter("staticSearch");
			if(staticSearch != null){
				String deptId = request.getParameter("deptId");
				String personId = request.getParameter("personId");
				String executeTime = request.getParameter("executeTime");
				SearchBean searchBean = new SearchBean();
				if(!StringUtil.isEmpty(deptId)){
					FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
					searchBean.add(field);
				}
				if(!StringUtil.isEmpty(personId)){
					FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
					searchBean.add(field);
				}
				if(!StringUtil.isEmpty(executeTime)){
					FieldBean field = new FieldBean("attendanceTime", executeTime);
					searchBean.add(field);
				}
				
				List<AttendanceItem> list = dao.findByFields(searchBean);
				HashMap<AttendanceItem, HashMap<Integer, String>> staticMap = new HashMap<AttendanceItem, HashMap<Integer, String>>();
				for(AttendanceItem item  : list){
					HashMap<Integer, String> attMap = new HashMap<Integer, String>();
					int result = item.getBean().getResult();
					attMap.put(result, "√");
					staticMap.put(item, attMap);
				}
				return staticMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
//		try {
//			AttendanceDAO dao = new AttendanceDAO();
//			SearchBean searchBean = buildSearchCondition(request, response, out);
//			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return 1;
	}
	
	public static String getAttendanceResult(HashMap<Integer, String> staticsMap, int attendanceType){
		if(staticsMap == null || staticsMap.size() == 0){
			return "";
		}else{
			return staticsMap.get(attendanceType) == null ? "" : staticsMap.get(attendanceType);
		}
	}
}
